
var port = 8887;
var io   = require('socket.io')(port);

var iScreen  = io.of('iScreen-socket');
var iControl = io.of('iControl-socket');
var sStore   = {
	socket:    null,
	timestamp: ''
};

// screen show end
iScreen.on('connect', function(socket) {
	console.log('iScreen connect');

	// 1.绑定操作事件
	socket.on('screen.operate', function(data) {
		iControl.emit('screen.operate', data);
	});
});

// mobile control end
iControl.on('connect', function(socket) {
	

	if(sStore.socket && sStore.socket.connected) {
		socket.emit('tips', 'Other people connected, please try later.');
		socket.disconnect();
		console.log('refuse');
		return;
	}else {
		sStore.socket    = socket;
		sStore.timestamp = new Date().getTime();

		console.log('control.connect');
		iScreen.emit('control.connected', {});
		socket.emit('control.connected', {});
	}

	// 2.绑定操作事件
	socket.on('control.operate', function(data) {
		iScreen.emit('control.operate', data);
	});

	// 3.绑定断开事件
	socket.on('disconnect', function() {
		iScreen.emit('control.disconnect', {});
	});
});

console.log('socket server on ' + port);